<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use App\Models\ConfigApp;
use Encore\Admin\Controllers\AdminController;

class ConfigAppsController extends AdminController
{
    protected $title = 'APP配置';
    
    protected function form()
    {
        $form = new Form(new ConfigApp());
	
	    $form->divider('App V1.0');
	    $this->formAppV1($form);
	    
        $this->formTools($form);
        $this->formSaved($form);
        $this->formFooter($form);

        return $form;
    }
    
    protected function formAppV1(Form $form)
    {
	    $form->text('name', 'APP名称');
	    $form->image('logo', 'LOGO')->uniqueName()->downloadable()->removable();
	    $form->image('bg_image', '背景图')->uniqueName()->downloadable()->removable();
	
	    $form->text('mini_appid', '付款小程序APPID')->rules('nullable|string');
	    $form->text('mini_pay_mark', '付款小程序标记')->rules('nullable|string')->help('NOTE: [wxpay, adapay, ...]');
	    
	    $form->radio('is_ios_on', '启用IOS')->options(config('site.bool'));
	    $form->text('pending_ios_version', 'IOS挂起版本');
	    $form->url('ios_url', 'IOS链接')->rules('nullable|string|max:255');
	    $form->url('android_url', 'ANDROID链接')->rules('nullable|string|max:255');
	    $form->text('version', 'Android 版本')->rules('required|regex:/^\d+\.\d+\.\d+$/')->help('版本格式：1.0.0');
	    $form->text('version_ios', 'IOS 版本')->rules('required|regex:/^\d+\.\d+\.\d+$/')->help('版本格式：1.0.0');
    }
	
	protected function formTools(Form $form)
	{
		$form->tools(function (Form\Tools $tools) {
			$tools->disableList();
			$tools->disableView();
			$tools->disableDelete();
		});
	}
	
	protected function formSaved(Form $form)
	{
		$form->saved(function () {
			admin_toastr('APP配置已更新', 'success');
			return redirect(route('admin.config-apps.edit', 1));
		});
	}
	
	protected function formFooter(Form $form)
	{
		$form->footer(function (Form\Footer $footer) {
			$footer->disableViewCheck();
			$footer->disableCreatingCheck();
			$footer->disableEditingCheck();
		});
	}
}
